//
//  HOTSwitchBtn.m
//  Genpara
//
//  Created by zhang chunhui on 14-9-22.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTSwitchBtn.h"

@implementation HOTSwitchBtn


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        
        [self initView];
        
    }
    return self;
}

-(void)awakeFromNib {
    
    [self initView];
}

-(void)initView {
    
    UIImage *bgImage = [UIImage imageNamed:@"switch_base"];
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    bgImageView.image = bgImage;
    
    [self addSubview:bgImageView];
    
    //_switchBlockBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _switchBlock = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"switch"]];
    _switchBlock.frame = CGRectMake(0, 0, 35, 34);
    _switchBlock.userInteractionEnabled = YES;
    //[_switchBlockBtn setBackgroundImage:[UIImage imageNamed:@"switch"] forState:UIControlStateNormal];
    [self addSubview:_switchBlock];
}

+(instancetype)defaultSwitchBtn {
    
    HOTSwitchBtn *switchBtn = [[[self class] alloc] initWithFrame:CGRectMake(0, 0, 64, 34)];
    
    return switchBtn;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    UITouch *touch = (UITouch *)[touches anyObject];
    
    if (touch.view == _switchBlock) {
        CGPoint location = [touch locationInView:_switchBlock];
        
        xx = location.x - _switchBlock.center.x;

        //_switchBlock.center = CGPointMake(xx, _switchBlock.center.y);
        
//        NSLog(@"====%f",xx);
    }
    
    
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    
    UITouch *touch = (UITouch *)[touches anyObject];
    
    if (touch.view == _switchBlock) {
        
        CGPoint location = [touch locationInView:self];
        //CGPoint location2 = [touch previousLocationInView:_switchBlock];
        
        float x = location.x - xx;
        
        if (xx < 0) {
            x = location.x;
        }
        
        if (x-_switchBlock.frame.size.width/2 < 0) {
            x = _switchBlock.frame.size.width/2;
        } else if(x + _switchBlock.frame.size.width/2 >= self.frame.size.width) {
            x = self.frame.size.width - _switchBlock.frame.size.width/2;
        }
        
        _switchBlock.center = CGPointMake(x, _switchBlock.center.y);
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    
    UITouch *touch = (UITouch *)[touches anyObject];
    
    if (touch.view == _switchBlock) {
        
        CGPoint location = [touch locationInView:self];
        CGPoint location2 = [touch previousLocationInView:_switchBlock];
        
        if (location2.x - _switchBlock.center.x == xx) {
            //点击事件
            [self setOn:!isOn animated:YES];
            return;
        }
        
        float x = location.x - xx;
        
        if (xx < 0) {
            x = location.x;
        }
        
        if (_switchBlock.center.x > self.frame.size.width/2) {
            
            [self setOn:YES animated:YES];
        } else {
            
            [self setOn:NO animated:YES];
        }
    }
}

-(void)setOn:(BOOL)flag animated:(BOOL)animated{
    
    isOn = flag;
    
    if (flag) {
        float x = self.frame.size.width - _switchBlock.frame.size.width/2;
        if (animated) {
            
            [UIView animateWithDuration:0.2 animations:^{
                _switchBlock.center = CGPointMake(x, _switchBlock.center.y);
                
            }];
        } else {
            _switchBlock.center = CGPointMake(x, _switchBlock.center.y);
        }
        
    } else {
        
        float x = _switchBlock.frame.size.width/2;
        if (animated) {
            
            [UIView animateWithDuration:0.2 animations:^{
                _switchBlock.center = CGPointMake(x, _switchBlock.center.y);
                
            }];
        } else {
            _switchBlock.center = CGPointMake(x, _switchBlock.center.y);
        }
    }
}

-(BOOL)getValue {
    
    return isOn;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
